package com.StarStudio.Controller;

import com.StarStudio.Service.CouponsStockService;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.Entity.*;
import com.ruoyi.common.core.utils.QueryPageUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
//@RequestMapping("/coupons_stock")
public class CouponsStockController {

    @Autowired
    CouponsStockService couponsStockService;

    /**
     * 优惠券库存分页方法
     * @param couponsStock 优惠券库存对象
     * @return 分页结果对象 PageResult
     */
    @GetMapping("/page")
    @RequiresLogin
    public PageResult Page(CouponsStock couponsStock){
        QueryPageUtils.startPage();
        PageInfo pageInfo;
        long allRecordNumber;
        List<CouponsStock> couponsStockList;
        try {
            couponsStockList = couponsStockService.findCouponsStockList(couponsStock);
            allRecordNumber = couponsStockService.findAllRecordNumber();
            pageInfo = new PageInfo<>(couponsStockList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.QUERY_PAGE_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.QUERY_PAGE_SUCCESS, pageInfo.getTotal(), couponsStockList, allRecordNumber);
    }

    /**
     * 导出优惠券库存详情信息到 Excel表格
     * @param response http响应
     * @param couponsStock 优惠券库存对象
     */
    @Log(title = "优惠券库存详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CouponsStock couponsStock) {
        List<CouponsStock> list = couponsStockService.findCouponsStockList(couponsStock);
        ExcelUtil<CouponsStock> util = new ExcelUtil<>(CouponsStock.class);
        util.exportExcel(response, list, "优惠券库存数据");
    }

    @PostMapping("/post")
    @RequiresLogin
    public Result addCouponsStock(@RequestBody CouponsStock couponsStock){
        try {
            couponsStockService.addCouponsStock(couponsStock);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ADD_COUPONS_STOCK_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.ADD_COUPONS_STOCK_SUCCESS);
    }

    @PutMapping("/put")
    @RequiresLogin
    public Result updateCouponsStock(@RequestBody CouponsStock couponsStock){
        try {
            couponsStockService.updateCouponsStock(couponsStock);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.UPDATE_COUPONS_STOCK_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.UPDATE_COUPONS_STOCK_SUCCESS);
    }

    /*
     *  远程调用接口
     */
    @RequestMapping("/UnsoldItemIds/{id}")
    List<Long> findUnsoldItemIdsByCouponsId(@PathVariable long id){
        List<Long> unsoldItemIdsByCouponsId = couponsStockService.findUnsoldItemIdsByCouponsId(id);
        if (CollectionUtils.isEmpty(unsoldItemIdsByCouponsId)){
            return null;
        }
        return unsoldItemIdsByCouponsId;
    }

    /*
     *  远程调用接口
     */
    @RequestMapping("/realDelete/{id}")
    void realDelete(@PathVariable(value = "id") long id){
        couponsStockService.realDelete(id);
    }

    /*
     *  远程调用接口
     */
    @RequestMapping("/updateDelFlag/{id}")
    void updateDelFlag(@PathVariable(value = "id") long id){
        couponsStockService.updateDelFlag(id);
    }

    /*
     *  远程调用接口
     */
    @RequestMapping("/SoldItemIds/{id}")
    List<Long> findSoldItemIdsByCouponsId(@PathVariable(value = "id") long id){
        List<Long> soldItemIdsByCouponsId = couponsStockService.findSoldItemIdsByCouponsId(id);
        if (CollectionUtils.isEmpty(soldItemIdsByCouponsId)){
            return null;
        }
        return soldItemIdsByCouponsId;
    }

    /*
     *  远程调用接口
     */
    @RequestMapping("/CouponsStockIds/{couponsId}")
    List<Long> findCouponsStockIds(@PathVariable(value = "couponsId") long couponsId){
        List<Long> couponsStockIds = couponsStockService.findCouponsStockIds(couponsId);
        if (CollectionUtils.isEmpty(couponsStockIds)){
            return null;
        }
        return couponsStockIds;
    }

    @RequestMapping("/UnsoldItems/{couponsId}/{flag}")
    List<CouponsStock> findUnsoldItems(@PathVariable(value = "couponsId") long couponsId,@PathVariable(value = "flag") boolean flag){
        List<CouponsStock> unsoldItems = couponsStockService.findUnsoldItems(couponsId, flag);
        if (CollectionUtils.isEmpty(unsoldItems)){
            return null;
        }
        return unsoldItems;
    }

    @RequestMapping("/SoldItems/{couponsId}/{flag}")
    List<CouponsStock> findSoldItems(@PathVariable(value = "couponsId") long couponsId, @PathVariable(value = "flag") boolean flag){
        List<CouponsStock> soldItems = couponsStockService.findSoldItems(couponsId, flag);
        if (CollectionUtils.isEmpty(soldItems)){
            return null;
        }
        return soldItems;
    }

    @RequestMapping("/findByOrderId/{orderId}")
    List<CouponsStock> findByOrder_id(@PathVariable(value = "orderId") long orderId){
        List<CouponsStock> byOrder_id = couponsStockService.findByOrder_id(orderId);
        if (CollectionUtils.isEmpty(byOrder_id)){
            return null;
        }
        return byOrder_id;
    }
}
